package com.lmc.form.script;

import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.val;
import org.apache.commons.lang.StringUtils;

import java.util.*;

public class ApiTable {
    public static class PagerInput{
        public List<ValueInfo> filters;
        public List<ValueInfo> orders;
        public PageInfo page;
    }
    @AllArgsConstructor
    @NoArgsConstructor
    private static class PageInfo{
        public int current;
        public int pageSize;
        public long total;
    }
    private static class ValueInfo{
        public String key;
        public String title;
        public Object value;
        public String type;
    }
    private PagerInput input;
    private ApiScript script;

    public ApiTable(ApiScript script, PagerInput input){
        this.script=script;
        this.input = input;
    }
    //获取输入的分页信息
    public PagerInput getPager(){
        return this.input;
    }
    //获取sql查询结果
    public Object querySql(String sql2,Object... ps){
        String sql = String.format(" from (%s) as lmtemp",sql2);
        val sqlParams=new ArrayList<Object>();
        for(val p:ps){
            sqlParams.add(p);
        }
        if(this.input.filters!=null&&this.input.filters.size()>0){
            String where = " where 1=1";
            for(val filter : this.input.filters){
                if(filter==null||filter.value==null||filter.value=="")
                    continue;
                if("Text".equals(filter.type)){
                    where+=" and (`"+filter.title+"` LIKE \"%\"?\"%\")";
                    sqlParams.add(filter.value);
                }
                if("Select".equals(filter.type)){
                    where+=" and (`"+filter.title+"`=?)";
                    sqlParams.add(filter.value);
                }
            }
            sql=sql+where;
        }
        if(this.input.orders!=null&&this.input.orders.size()>0){
            String orderBy="";
            for(val order : this.input.orders){
                if(order==null)
                    continue;
                if(order.value!=null&&order.value instanceof String){
                    if("asc".equalsIgnoreCase((String)order.value)){
                        orderBy += " `"+ order.title +"` asc,";
                    }
                    if("desc".equalsIgnoreCase((String)order.value)){
                        orderBy += " `"+ order.title +"` desc,";
                    }
                }
            }
            if(orderBy!="") {
                orderBy = " order by " + StringUtils.strip(orderBy, ",");
                sql = sql + orderBy;
            }
        }
        String selectSql = "select * " +sql;
        String countSql="select count(0) as num "+sql;
        if(this.input.page!=null){
            if(this.input.page.current>0&&this.input.page.pageSize>0){
                String limit =" limit "+(this.input.page.current-1)*this.input.page.pageSize+","+this.input.page.pageSize;
                selectSql=selectSql+limit;
            }
        }
        val list = this.script.querySql(selectSql,sqlParams.toArray());
        List<Map<String,Object>> count = (List<Map<String,Object>>)this.script.querySql(countSql,sqlParams.toArray());
        HashMap map = new HashMap<String,Object>();
        map.put("data",list);
        map.put("page",new PageInfo(this.input.page.current,this.input.page.pageSize,(long) count.get(0).get("num")));
        return map;
    }


}
